

""" This Class controls the flow of the game. It allows you to change
states in the game while keeping the previous state preserved to be
resumed. The state machine will always return the active state with the
top() method. the flow control of the states is controled by the code 
that instances it.
"""
class Stack:
	def __init__(self):
		self.data = []
	
	# This method pushes a state onto the stack, making it the top and the method top()
	# will return this on the next call.
	def push(self, data_):
		self.data.append(data_)
		
	# This method removes the current state from the top of the stack. The state below it 
	# will automaticly be retured by the next call to the top() method.
	def pop(self):
		data = self.top()
		self.data.pop(-1)
		return data
		
	# Returns the number of states in the stack
	def count(self):
		return len(self.data)
		
	# returns the top state in the stack (the active one) 
	# TODO: Must clean this code. The stack can run out of states and this needs
	# to be handled.
	def top(self):
		if len(self.data) == 0:
			#print "Stack empty: handle exception here"
			return 0
		else: 
			return self.data[-1]
			
	# Dumps some data about the stack.
	def dump(self):
		if len(self.data) == 0:
			return 0
		print "Stack: length=", len(self.data)
		for x in self.data:
			print x.name, " run=", x.run, " needsInit=", x.needsInit
		topState = self.top()
		print "Top is " + topState.name
		
	# This method checks to see if the topstate has its ready flag set.
	def ready(self):
		if len(self.data) == 0:
			return 0
		else:
			topState = self.top()
			return topState.run
	
	# Checks to see if the state needs to be inited.
	# TODO: this is worthless. please remove.
	def needInit(self):
		topState = self.top()
		return topState.needsInit
	
	# Cleans up the stack.
	# TODO: This function needs to be able to look through the stack
	# and pick out states that are left behind or that have had their
	# ready flag changed by the current state.
	def cleanup(self):
		if self.ready():
			pass
	
	
	def __del__(self):
		pass
		
